/**
 * Created by isoft-work 5 on 12/28/2014.
 */

$(document).ready(function(){
//   render
   lstProduct = null;
   _filterPrice = null;
   _textFilterPrice = null;
   _filterSize = null;
   _danhMuc = null;
   _currentDM = null;
   _currentType = '0';
   renderConsultantClothes();
//   event
   bindEventConsultantPage();
});

function renderConsultantClothes() {
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   if(!useName) {
      ISS.WINDOW.location('/jsp/user/user-login.jsp');
   }
   else {
      getSupposedConsultant();
      renderCatalogSize(CONSTANT.ConsultantClothes.ListSize);
   }
}

function renderSoDoCoThe(type) {
   hideAllSoDoCoThe();
   // áo
   if(type) {
      _currentType = type;
      if(type == CONSTANT.ConsultantClothes.TypeAo[0]) {
         var _gender = ISS.SESSION.getSession('session_gender');
         if(_gender == 'NAM') {
            ISS.DOM.show('#input_member_breast_show');
            ISS.DOM.show('#input_member_shoulder_show');
            ISS.DOM.show('#input_member_back_show');
         }
         else {
            ISS.DOM.show('#input_member_breast_show');
            ISS.DOM.show('#input_member_shoulder_show');
            ISS.DOM.show('#input_member_back_show');
            ISS.DOM.show('#input_member_waist_show');
         }
      }

      if(type == CONSTANT.ConsultantClothes.TypeAo[1]) {
         ISS.DOM.show('#input_member_breast_show');
         ISS.DOM.show('#input_member_shoulder_show');
         ISS.DOM.show('#input_member_back_show');
      }
   }
   // quần
   else {
      _currentType = CONSTANT.ConsultantClothes.TypeAo[3];
      var _gender = ISS.SESSION.getSession('session_gender');
      if(_gender == 'NAM') {
         ISS.DOM.show('#input_member_waist_show');
         ISS.DOM.show('#input_member_butt_show');
         ISS.DOM.show('#input_member_femur_show');
      }
      else {
         ISS.DOM.show('#input_member_waist_show');
         ISS.DOM.show('#input_member_butt_show');
         ISS.DOM.show('#input_member_leg_show');
      }
   }
}

function hideAllSoDoCoThe() {
   ISS.DOM.hide('#input_member_breast_show');
   ISS.DOM.hide('#input_member_waist_show');
   ISS.DOM.hide('#input_member_butt_show');
   ISS.DOM.hide('#input_member_back_show');
   ISS.DOM.hide('#input_member_arm_show');
   ISS.DOM.hide('#input_member_shoulder_show');
   ISS.DOM.hide('#input_member_femur_show');
   ISS.DOM.hide('#input_member_leg_show');
}

function renderCatalogSize(kichThuoc) {
   var catalogSize = '';
   for(var i = 0; i < kichThuoc.length; i++) {
      catalogSize += ISS.STRING_UTIL.format(TEMPLATE.Product.catalogSize,{
         tenKichThuoc: kichThuoc[i].tenSize,
         id: kichThuoc[i].tenSize
      });
   }
   ISS.DOM.html('#filter_price', catalogSize);
   bindEventCatalogSize(kichThuoc);
}

function bindEventCatalogSize(kichThuoc) {
   for(var i = 0; i < kichThuoc.length; i++) {
      ISS.EVENT.bindEvent('#' + kichThuoc[i].tenSize, 'click', function () {
         _filterSize = this.id;
         filterProductSize(_filterSize);
      });
   }
   // event filter price
   for(i = 0; i < 6; i++) {
      ISS.EVENT.bindEvent('#filter_price_' + i, 'click', function () {
         var values = ISS.DOM.getData(this,'cost');
         _filterPrice = values;
         _textFilterPrice = this.textContent;
         filterProductPrice(values, _textFilterPrice);
      });
   }
}

function getConsultantUser(useName, gender) {
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/consultant/getConsultantByUserId/' + useName + '/' + gender,
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getConsultantUserComplete(responseInfo);
         ISS.CONTROL.unLockScreen('#content');
      }
   });
}

function getConsultantUserComplete(consultant) {
   if(consultant) {
      mapUserInfoToView(consultant);
   }
}

function mapUserInfoToView(consultant) {
   var _soDoCoThe = consultant.soDoCoThe;
   ISS.DOM.setVal('#input_member_height', _soDoCoThe.sdChieuCao);
   ISS.DOM.setVal('#input_member_weight', _soDoCoThe.sdCanNang);

   getBMICode(_soDoCoThe.sdCanNang, _soDoCoThe.sdChieuCao);

   ISS.DOM.setAtt('#'+consultant.mauDa, 'checked', 'true');

   mapSoDoCoTheToView(_soDoCoThe);
}

function mapSoDoCoTheToView(soDoCoThe) {
   var _sdNguc = soDoCoThe.sdNguc || '',
      _sdEo = soDoCoThe.sdEo || '',
      _sdMong = soDoCoThe.sdMong || '',
      _sdDaiLung = soDoCoThe.sdDaiLung || '',
      _sdDaiTay = soDoCoThe.sdDaiTay || '',
      _sdVai = soDoCoThe.sdVai || '',
      _sdDui = soDoCoThe.sdDui || '',
      _sdDaiChan = soDoCoThe.sdDaiChan || '';
   ISS.DOM.setVal('#input_member_breast', _sdNguc);
   ISS.DOM.setVal('#input_member_waist',  _sdEo);
   ISS.DOM.setVal('#input_member_butt', _sdMong);
   ISS.DOM.setVal('#input_member_back', _sdDaiLung);
   ISS.DOM.setVal('#input_member_arm', _sdDaiTay);
   ISS.DOM.setVal('#input_member_shoulder', _sdVai);
   ISS.DOM.setVal('#input_member_femur', _sdDui);
   ISS.DOM.setVal('#input_member_leg', _sdDaiChan);
}

function mapListProductToView(listProduct) {
   // show list product
   var _product = '';
   for(var i = 0; i < listProduct.length; i++) {
      var size = '', cost, image;
      var _chiTietSanPhams = listProduct[i].chiTietSanPhams,
         _listSize = listProduct[i].size;
      for(var s = 0; s < _listSize.length; s++) {
         size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
            tenSize: _listSize[s].tenSize
         })
      }
      for(var c = 0; c < _chiTietSanPhams.length; c++) {
         if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
            // check date sale off
            var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
            var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
            if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[c].giaSP
               });
               image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                  image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                  off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
               });
               break;
            }
            else {
               if(c == _chiTietSanPhams.length - 1) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[0].giaSP
                  });
                  if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                        off: '0'
                     });
                  }
                  else {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath'),
                        off: '0'
                     });
                  }
               }
            }
         }
         else {
            if(c == _chiTietSanPhams.length - 1) {
               cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                  cost: _chiTietSanPhams[0].giaSP
               });
               if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                     off: '0'
                  });
               }
               else {
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath'),
                     off: '0'
                  });
               }
            }
         }
      }
      _product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
         itemDetailSize: size,
         image: image,
         tenSP: listProduct[i].tenSP,
         thongTin: listProduct[i].thongTin,
         cost: cost,
         detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
      });
   }
   ISS.DOM.html('#productsCatalog', _product);
   ISS.DOM.paging('#recommentClothes');
}

function getSupposedConsultant() {
   ISS.CONTROL.lockScreen('#content');
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/consultant/getSupposed/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'GET',
      success: function (data) {
         var responseInfo = data.responseObject;
         getSupposedConsultantComplete(responseInfo);
      }
   });
}

function getSupposedConsultantComplete(supposeInfo) {
   var _html = TEMPLATE.ConsultantClothes.header;
   var _mauDa = supposeInfo.thongTinTuVans;
   // mau da
   for(var i = 0; i < _mauDa.length; i++) {
      var _suppose = _mauDa[i];
      var _id = _suppose.maTT.substr(0,2).toLocaleUpperCase();
      if(_id == CONSTANT.ConsultantClothes.MauDa) {
         _html += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ConsultantClothes.listInfo, _suppose);
      }
   }
   ISS.DOM.html('#my-color-skin-table', _html);

//   danh muc san pham
   _danhMuc = supposeInfo.loaiDanhMucFulls;
   _currentDM = _danhMuc[0].maLoai;
   var _danhmucHtml = '';
   for(i = 0; i < _danhMuc.length; i++) {
      _danhmucHtml += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ConsultantClothes.listOptionLoaiDM, _danhMuc[i]);
   }
   maploaiDanhMucSanPhamToView(_danhMuc[0].loaiDanhMucSanPham);

   ISS.DOM.html('#consultant_list_product', _danhmucHtml);

   // get so do co the
   var _gender = ISS.SESSION.getSession('session_gender');
   var useName = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
   getConsultantUser(useName, _gender);

}

function maploaiDanhMucSanPhamToView (loaiDanhMucSanPham) {
   var _danhmucDetailHtml = '';
   for(var j = 0; j < loaiDanhMucSanPham. length; j ++) {
      _danhmucDetailHtml += ISS.TEMPLATE.handleBar.renderByString(TEMPLATE.ConsultantClothes.listOptionLoaiDMSP, loaiDanhMucSanPham[j]);
   }
   ISS.DOM.html('#consultant_list_product_detail', _danhmucDetailHtml);
}

function bindEventConsultantPage() {
   ISS.EVENT.bindEvent('#Consultant_Button', 'click', function () {
      var _result = {};
      if(validateConsultant(_result)){
         getConsultant(_result);
      }
   });

   ISS.EVENT.bindEvent('#consultant_list_product_detail_type', 'change', function () {
      var _value = ISS.DOM.getVal('#' + this.id);
      renderSoDoCoThe(_value);
   });

   ISS.EVENT.bindEvent('#consultant_list_product', 'change', function () {
      var _value = ISS.DOM.getVal('#' + this.id);
      if(_value == _danhMuc[0].maLoai){
         _currentDM = _danhMuc[0].maLoai;
         maploaiDanhMucSanPhamToView(_danhMuc[0].loaiDanhMucSanPham);
         ISS.DOM.show('#consultant_list_product_detail_type');
         hideAllSoDoCoThe();
      }
      else {
         _currentDM = _danhMuc[1].maLoai;
         maploaiDanhMucSanPhamToView(_danhMuc[1].loaiDanhMucSanPham);
         ISS.DOM.hide('#consultant_list_product_detail_type');
         renderSoDoCoThe();
      }
   });

   ISS.EVENT.bindEvent('#input-member-height', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]|[.]/);
   });

   ISS.EVENT.bindEvent('#input-member-weight', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]|[.]/);
   });

   ISS.EVENT.bindEvent('#input_member_breast', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_waist', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_butt', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_back', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_arm', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_shoulder', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_femur', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   ISS.EVENT.bindEvent('#input_member_leg', 'keypress', function (e) {
      ISS.VALIDATION.allowInput(e, /[0-9]/);
   });

   // event remove filter
   ISS.EVENT.bindEvent('#remove_filter_size', 'click', function () {
      removeFilterSize();
   });

   ISS.EVENT.bindEvent('#remove_filter_price', 'click', function () {
      removeFilterPrice();
   });
}

function mapViewToSoDoCoThe(value) {

   value.soDoCoThe.sdNguc = ISS.DOM.getVal('#input_member_breast');
   value.soDoCoThe.sdEo = ISS.DOM.getVal('#input_member_waist');
   value.soDoCoThe.sdMong = ISS.DOM.getVal('#input_member_butt');
   value.soDoCoThe.sdDaiLung = ISS.DOM.getVal('#input_member_back');
   value.soDoCoThe.sdDaiTay = ISS.DOM.getVal('#input_member_arm');
   value.soDoCoThe.sdVai = ISS.DOM.getVal('#input_member_shoulder');
   value.soDoCoThe.sdDui = ISS.DOM.getVal('#input_member_femur');
   value.soDoCoThe.sdDaiChan = ISS.DOM.getVal('#input_member_leg');
}

function validateConsultant(result) {
   if(validateAndGetGT(result)){
      if(validateAndGetSD(result)) {
         mapViewToSoDoCoThe(result);
         return true;
      }
      else {
         alert('Hãy nhập đầy đủ số đo cơ thể');
         return false;
      }
   }
   else {
      alert('Hãy chọn đầy đủ số giả thuyết');
      return false;
   }
}

function validateAndGetSD(result) {
   result.typeSize = _currentType;
   if(_currentDM == _danhMuc[0].maLoai) {
      // áo sơ mi
      if(_currentType == CONSTANT.ConsultantClothes.TypeAo[0]) {
         var _gender = ISS.SESSION.getSession('session_gender');
         // nam
         if(_gender == 'NAM') {
            var _sdNguc = ISS.DOM.getVal('#input_member_breast');
            var _sdVai = ISS.DOM.getVal('#input_member_shoulder');
            var _sdDaiLung = ISS.DOM.getVal('#input_member_back');
            if(!_sdNguc || !_sdVai || !_sdDaiLung) {
               return false;
            }
            else {
               return true;
            }
         }
         // nu
         else {
            var _sdNguc = ISS.DOM.getVal('#input_member_breast');
            var _sdVai = ISS.DOM.getVal('#input_member_shoulder');
            var _sdDaiLung = ISS.DOM.getVal('#input_member_back');
            var _sdEo = ISS.DOM.getVal('#input_member_waist');
            if(!_sdNguc || !_sdVai || !_sdDaiLung || !_sdEo) {
               return false;
            }
            else {
               return true;
            }
         }
      }
      // áo thun
      if(_currentType == CONSTANT.ConsultantClothes.TypeAo[1]) {
         var _sdNguc = ISS.DOM.getVal('#input_member_breast');
         var _sdVai = ISS.DOM.getVal('#input_member_shoulder');
         var _sdDaiLung = ISS.DOM.getVal('#input_member_back');
         if(!_sdNguc || !_sdVai || !_sdDaiLung) {
            return false;
         }
         else {
            return true;
         }
      }
      // chon khac
      if(_currentType == CONSTANT.ConsultantClothes.TypeAo[2]) {
         return true;
      }
      // chua chon
      return false;
   }
   // quần
   else {
      var _gender = ISS.SESSION.getSession('session_gender');
      var _sdEo = ISS.DOM.getVal('#input_member_waist');
      var _sdMong = ISS.DOM.getVal('#input_member_butt');
      if(_gender == 'NAM') {
         var _sdDui = ISS.DOM.getVal('#input_member_femur');
         if(!_sdEo || !_sdMong || !_sdDui) {
            return false;
         }
         else {
            return true;
         }
      }
      else {
         var _sdDaiChan = ISS.DOM.getVal('#input_member_leg');
         if(!_sdEo || !_sdMong || !_sdDaiChan) {
            return false;
         }
         else {
            return true;
         }
      }
   }
}

function validateAndGetGT(result) {
   var r;
   var _mauDa = ISS.DOM.getVal('#my-color-skin-table input[name=input-skin-color]:checked'),
      _height = ISS.DOM.getVal('#input_member_height'),
      _weight = ISS.DOM.getVal('#input_member_weight');
   if(!_mauDa){
      r = false;
   }
   if(!_weight) {
      r = false;
   }
   if(!_height) {
      r = false;
   }

   if(_mauDa && _weight && _height) {
      if(ISS.VALIDATION.isNumber(_height) && ISS.VALIDATION.isNumber(_weight)) {
         if(_weight >= 20 && _height >= 1.45 && _height <= 3) {
            // get BMI
            var _codeBMI = getBMICode(_weight, _height);
            var user = {};
            user.maTV = ISS.SESSION.getSession('userName') ? ISS.SESSION.getSession('userName') : ISS.COOKIE.getCookie('userName');
            var _soDoCoThe = {};
            _soDoCoThe.sdChieuCao =  _height;
            _soDoCoThe.sdCanNang = _weight;
            _soDoCoThe.thongTinThanhVien = user;
            result.soDoCoThe = _soDoCoThe;
            result.tapGiaThiet = _mauDa + '^' + _codeBMI;
            result.gioiTinhTuVan = ISS.SESSION.getSession('session_gender');
            result.danhMucSP = ISS.DOM.getVal('#consultant_list_product_detail');
            r = true;
         }
         else {
            r = false;
         }
      }
      else {
         r = false;
      }
   }
   return r;
}

function getBMICode(weight, height) {
   var _BMINumber = weight/(height*height);
   var _lstBMI = CONSTANT.ConsultantClothes.BMI_Value;

   for(var i = 0; i <= _lstBMI.length; i++ ) {
      if(_BMINumber < _lstBMI[i]) {
         var _codeBMI = CONSTANT.ConsultantClothes.BMI + (i + 1);
         mapBIMReSult(i);
         break;
      }
      else {
         if(i == _lstBMI.length)
            _codeBMI = CONSTANT.ConsultantClothes.BMI + (i + 1);
         mapBIMReSult(i);
      }
   }
   return _codeBMI;
}

function mapBIMReSult (i) {
   ISS.DOM.text('#result_bmi', 'Thể Trạng: ' + CONSTANT.ConsultantClothes.BMI_Result[i]);
}

function getConsultant(result) {
   ISS.CONTROL.lockScreen('#consultant-form');
   ISS.BACKEND.request({
      url: ISS.SESSION.getSession('contextPath') + '/service/consultant/getConsultant/',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(result),
      success: function (data) {
         var responseInfo = data.responseObject;
         _listSuppose = responseInfo;
         getConsultantComplete(responseInfo);
         ISS.CONTROL.unLockScreen('#consultant-form');
      }
   });
}

function getConsultantComplete(consultant) {
   lstProduct = consultant.sanPhams;
   mapListProductToView(consultant.sanPhams);
}

function removeFilterSize() {
   _filterSize = null;

   ISS.DOM.hide('#facet_size_system');
   if(_textFilterPrice) {
      var beginCost = _filterPrice.substring(0,_filterPrice.indexOf('-')),
         endCost = _filterPrice.substring(_filterPrice.indexOf('-') + 1, _filterPrice.length);
      showProductAgainFilterPrice(beginCost, endCost);
   }
   else {
      mapListProductToView(lstProduct);
   }
}

function removeFilterPrice() {
   _filterPrice = null;
   _textFilterPrice = null;

   ISS.DOM.hide('#facet_price_system');
   if(_filterSize) {
      showProductAgainFilterSize(_filterSize);
   }
   else {
      mapListProductToView(lstProduct);
   }
}

function filterProductPrice(values) {
   var beginCost = values.substring(0,values.indexOf('-')),
      endCost = values.substring(values.indexOf('-') + 1, values.length);
   if(_filterSize) {
      showProductAgainFilterSizeAndPrice(_filterSize, beginCost, endCost);
   }
   else {
      showProductAgainFilterPrice(beginCost, endCost);
   }

   ISS.DOM.text('#name_price_selected', _textFilterPrice);
   ISS.DOM.show('#facet_price_system');
}

function filterProductSize(filterSize) {
   if(_textFilterPrice) {
      var beginCost = _filterPrice.substring(0, _filterPrice.indexOf('-')),
         endCost = _filterPrice.substring(_filterPrice.indexOf('-') + 1, _filterPrice.length);
      showProductAgainFilterSizeAndPrice(_filterSize, beginCost, endCost);
   }
   else {
      showProductAgainFilterSize(filterSize);
   }

   ISS.DOM.text('#name_size_selected', ISS.DOM.getText('#' + _filterSize));
   ISS.DOM.show('#facet_size_system');
}

function showProductAgainFilterSize(maSize) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image ;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      for(var t = 0; t < _listSize.length; t++) {
         if(_listSize[t].tenSize == maSize) {
            for(var s = 0; s < _listSize.length; s++) {
               size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
                  tenSize: _listSize[s].tenSize
               })
            }
            for(var c = 0; c < _chiTietSanPhams.length; c++) {
               if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
                  // check date sale off
                  var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
                  var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
                  if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[c].giaSP
                     });
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                        off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                     });
                     break;
                  }
                  else {
                     if(c == _chiTietSanPhams.length - 1) {
                        cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                           cost: _chiTietSanPhams[0].giaSP
                        });
                        if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                              off: '0'
                           });
                        }
                        else {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath'),
                              off: '0'
                           });
                        }
                     }
                  }
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
               itemDetailSize: size,
               image: image,
               tenSP: lstProduct[i].tenSP,
               thongTin: lstProduct[i].thongTin,
               cost: cost,
               detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
            });
            break;
         }
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function showProductAgainFilterPrice(begin, end) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      for(var t = 0; t < _chiTietSanPhams.length; t++) {
         if(_chiTietSanPhams[t].giaSP >= begin && _chiTietSanPhams[t].giaSP <= end) {
            for(var s = 0; s < _listSize.length; s++) {
               size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
                  tenSize: _listSize[s].tenSize
               })
            }
            for(var c = 0; c < _chiTietSanPhams.length; c++) {
               if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
                  // check date sale off
                  var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
                  var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
                  if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[c].giaSP
                     });
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                        off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                     });
                     break;
                  }
                  else {
                     if(c == _chiTietSanPhams.length - 1) {
                        cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                           cost: _chiTietSanPhams[0].giaSP
                        });
                        if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                              off: '0'
                           });
                        }
                        else {
                           image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                              image: ISS.SESSION.getSession('contextPath'),
                              off: '0'
                           });
                        }
                     }
                  }
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
               itemDetailSize: size,
               image: image,
               tenSP: lstProduct[i].tenSP,
               thongTin: lstProduct[i].thongTin,
               cost: cost,
               detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
            });
            break;
         }
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function showProductAgainFilterSizeAndPrice(maSize, begin, end) {
   var product = '';
   for(var i = 0; i < lstProduct.length; i++) {
      var size = '', cost, image ;
      var _chiTietSanPhams = lstProduct[i].chiTietSanPhams,
         _listSize = lstProduct[i].size;
      if(checkSizeAndPrice(_chiTietSanPhams, _listSize, maSize, begin, end)) {
         for(var s = 0; s < _listSize.length; s++) {
            size += ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailSize,{
               tenSize: _listSize[s].tenSize
            })
         }
         for(var c = 0; c < _chiTietSanPhams.length; c++) {
            if(_chiTietSanPhams[c].chiTietKhuyenMai.length > 0) {
               // check date sale off
               var _ngayBD = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayBD;
               var _ngayKT = _chiTietSanPhams[c].chiTietKhuyenMai[0].khuyenMai.ngayKT;
               if(ISS.DATE.isBetWeen(_ngayBD, _ngayKT)) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[c].giaSP
                  });
                  image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                     image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[c].hinhAnhSanPham[0].tenHinh,
                     off: _chiTietSanPhams[c].chiTietKhuyenMai[0].phanTram
                  });
                  break;
               }
               else {
                  if(c == _chiTietSanPhams.length - 1) {
                     cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                        cost: _chiTietSanPhams[0].giaSP
                     });
                     if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                           off: '0'
                        });
                     }
                     else {
                        image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                           image: ISS.SESSION.getSession('contextPath'),
                           off: '0'
                        });
                     }
                  }
               }
            }
            else {
               if(c == _chiTietSanPhams.length - 1) {
                  cost = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailCost,{
                     cost: _chiTietSanPhams[0].giaSP
                  });
                  if(_chiTietSanPhams[0].hinhAnhSanPham.length > 0) {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath') + _chiTietSanPhams[0].hinhAnhSanPham[0].tenHinh,
                        off: '0'
                     });
                  }
                  else {
                     image = ISS.STRING_UTIL.format(TEMPLATE.Product.itemDetailImage,{
                        image: ISS.SESSION.getSession('contextPath'),
                        off: '0'
                     });
                  }
               }
            }
         }
         product += ISS.STRING_UTIL.format(TEMPLATE.Product.item,{
            itemDetailSize: size,
            image: image,
            tenSP: lstProduct[i].tenSP,
            thongTin: lstProduct[i].thongTin,
            cost: cost,
            detailPage: ISS.SESSION.getSession('contextPath') + '/service/product/toPageProductDetail/' + lstProduct[i].maSP
         });
      }
   }
   ISS.DOM.html('#productsCatalog', product);
   ISS.DOM.paging('#catalog_container');
}

function checkSizeAndPrice(chiTietSanPhams, listSize, maSize, begin, end) {
   for (var s = 0; s < listSize.length; s++) {
      if(listSize[s].tenSize == maSize) {
         for(var t = 0; t < chiTietSanPhams.length; t++) {
            if (chiTietSanPhams[t].giaSP >= begin && chiTietSanPhams[t].giaSP <= end) {
               return true;
            }
         }
      }
      else {
         if(s == listSize.length - 1) {
            return false;
         }
      }
   }
}


